package graphic;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.JPanel;

import listener.physical.BallCollision;
import listener.physical.WallCollision;
import model.GlobalParameters;
import model.Particle;


public class GamePanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4280123044832216825L;
	private LinkedList<Particle> particleList;
	
	public GamePanel(){
		setParticleList(new LinkedList<Particle>());
	}

	public LinkedList<Particle> getParticleList() {
		return particleList;
	}


	public void setParticleList(LinkedList<Particle> particleList) {
		this.particleList = particleList;
	}
	
	public void addParticle(Particle p){
		particleList.add(p);
		p.addObserver(BallCollision.getInstance());
		p.addObserver(WallCollision.getInstance());
	}


	public void clearScreen(Graphics graphic){
		graphic.setColor(Color.white);
		graphic.fillRect(0, 0, GlobalParameters.Width, GlobalParameters.Height);
	}
	
	public void moveParticles(double dt){
		for(Particle p : particleList){
			p.move(dt);
		}
	}
	
	public void removeParticles(){
		LinkedList<Particle> toRemove = new LinkedList<Particle>();
		for(Particle p : particleList){
			if(p.isDead()){
				toRemove.add(p);
				BallCollision.removeParticle(p);
			}
		}
		particleList.removeAll(toRemove);
		
	}
	
	public void agePlus(){
		for(Particle p : particleList){
			p.setAge(p.getAge()+1);
		}
	}

	@Override
	public void paint(Graphics graphic) {
		clearScreen(graphic);
		for(Particle p : particleList){
			p.paint(graphic);
		}
	}
	
	
}
